'use strict';

app.controller('users',['$stateParams','$state','$filter','$window','$http','$scope','$modal','toaster','$log', function($stateParams,$state,$filter,$window,$http,$scope,$modal,toaster,$log) {
    function inIt(){
        if($stateParams.docId===null){
            getDeptList();
            getUserList();
            $scope.myShow = false;
        }else{//$stateParams.docId
            toaster.getInfo('/a_yongan/single/user/'+$stateParams.docId,null).then(function(data){
                if(data && data.data && data.data.data){
                    makeData([data.data.data],true);
                }
            });
            $scope.myShow = true;

        }
    }
        $scope.title = {
            title:'用户列表信息',
            addTitle:'员工信息'
        };
        $scope.toaster = {
            type: 'warning',
            title: '提示：',
            text: '内容不能为空！'
        };
    function getDeptList(){
        toaster.getInfo('/a_yongan/selectlist/dept',null).then(function(data){
            if(data && data.data && data.data.data){
                $scope.zNodes =data.data.data;
                madeTree();
            }
        });
    }
    function getUserList(){
        toaster.getInfo('/a_yongan/list/user',null).then(function(data){
            if(data && data.data && data.data.data){
                makeData(data.data.data);
                setTimeout(function(){
                    $('.table').trigger('footable_redraw');
                }, 100);
            }
        });
    }

    function makeData(data,single){
        data.forEach(function (item) {
            item.createTime = $filter("date")(item.createTime, "yyyy/MM/dd");
            if(item.qy=='true'){
                item.qy = true;
                item.status = '使用中...';
                item.status1 = 'label bg-info';
            }else{
                item.qy = false;
                item.status = '禁用中...';
                item.status1 = 'label bg-danger';
            }
        });
        if(single){
            $scope.showInfo(data[0]);
        }else{
            $scope.saveDate = data;
            $scope.saveDateAll = data;
        }
    }
    $scope.lists = ["男","女"];
    $scope.mylabel = $scope.lists[0];//默认数组第一个作为option的值
        $scope.usersAdd = false;
        $scope.usersList = true;
        $scope.userRow = {};
        $scope.showInfo = function(row){
            $scope.usersAdd = !($scope.usersAdd);
            $scope.usersList = !($scope.usersList);
            $scope.userRow = row;
            $scope.mylabel = $scope.userRow.userSex;
        };
        $scope.jumpToUrl = function(ts) {
            if(ts!=='back'){
                $scope.usersAdd = !($scope.usersAdd);
                $scope.usersList = !($scope.usersList);
                $scope.userRow = {
                    'userNumber':'USER'+$filter("date")(new Date(), "yyyyMMddHHmmss"),
                    "qy":false,
                    "deptId":$scope.selectNode.id || "",
                    "dept":{
                        "deptName":$scope.selectNode.name || ""
                    }
                };
                $scope.mylabel = $scope.lists[0];
                madeTree();
            }else{
                $scope.usersAdd = !($scope.usersAdd);
                $scope.usersList = !($scope.usersList);
                getDeptList();
                getUserList();
            }

        };
        $scope.columns = [
            {
                title:'员工编号',
                show:true
            },
            {
                title:'员工姓名',
                show:true
            },
            {
                title:'所属部门',
                show:true
            },
            {
                title:'职务',
                show:true
            },
            {
                title:'联系电话',
                show:true
            },
            {
                title:'创建时间',
                show:true
            },
            {
                title:'员工状态',
                show:true
            }
        ];
        $scope.saveDate=[];
        $scope.saveDateAll=[];
        $scope.doing_async = true;
        $scope.setting = {
            data: {
                simpleData: {
                    enable: true
                }
            },
            callback: {
                beforeClick: showDetail
            }
        };
        $scope.selectNode = {};
        function showDetail(treeId, treeNode, clickFlag){
            //此处进行列表筛选，显示子节点列表
            //var saveDateLS = [];
            $scope.saveDate = [];
            $scope.selectNode = treeNode;
            $scope.saveDateAll.forEach(function(item){
                if(treeNode.id==1){
                    $scope.saveDate.push(item);
                }else{
                    if(treeNode.id==item.deptId){
                        $scope.saveDate.push(item);
                    }
                }
            });
            $scope.$apply();
            setTimeout(function(){
                $('.table').trigger('footable_redraw');
            }, 100);
        }
        $scope.zNodes =[];
        function madeTree(){
            setTimeout(function() {
                $.fn.zTree.init($("#mytree"), $scope.setting, $scope.zNodes);
            },100);
            $scope.doing_async = false;
        }
        $scope.showSelectDept = function(id){
        var modalInstance = $modal.open({
            templateUrl: 'selectDept.html',
            controller: 'selectDeptCtrl',
            size: 'sm',
            resolve: {
                items: function () {
                    return $scope.message;
                }
            }
        });
        modalInstance.result.then(function (selectedItem) {
            $('#'+id+'').val(selectedItem.name);
            $('#deptId').val(selectedItem.id);
            showMessage('success','','选择成功！');
            //$scope.selected = selectedItem;
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    };

        $scope.saveInfo = function (size) {
            if(!checkItems()){
                return;
            }
            var modalInstance = $modal.open({
                templateUrl: 'myModalContent.html',
                controller: 'ModalInstanceCtrl',
                size: size,
                resolve: {
                    items: function () {
                        return $scope.message;
                    }
                }
            });
            modalInstance.result.then(function (selectedItem) {
                var info = addDo();
            }, function () {
                $log.info('Modal dismissed at: ' + new Date());
            });
        };
        function checkItems(){
            var items = ['userName','userNum','userId','dept'];
            var itemsValue = ['员工姓名','单位名称','用户ID','机构名称'];
            for(var i=0;i<items.length;i++){
                if($('#'+items[i]+'').val()===''){
                    $('#'+items[i]+'').focus();
                    showMessage('warning','',itemsValue[i]+"不能为空！");
                    return false;
                }
            }
            return true;
        }
    $scope.toPY = function(){
        if(!$scope.userRow.userId){
            var yinwen = $("#userName").toPinyin();
            yinwen = yinwen.replace(/\s+/g,"");
            yinwen = yinwen.toLocaleLowerCase();
            $("#userEname").val(yinwen);
        }
    }
        $scope.message = '是否确定保存？';
        function showMessage(type,title,text){
            $scope.toaster.type = type;
            $scope.toaster.text = text;
            toaster.pop($scope.toaster.type, $scope.toaster.title, $scope.toaster.text);
        }
    function addDo(){
        var notesdata = {};
        if(!$scope.userRow.userId){
            notesdata = {
                "userId":"",
                "qy":$("#qy").prop("checked")+"",
                "zdr":$scope.app.userName,
                "zdrId":$scope.app.userId,
                "userNumber":$("#userNumber").val(),
                "userName":$("#userName").val(),
                "deptId":$("#deptId").val(),
                "userSex":$scope.lists[$("#userSex").val()],
                "userJob":$("#userJob").val(),
                "userTel":$("#userTel").val(),
                "userEducation":$("#userEducation").val(),
                "userSchool":$("#userSchool").val(),
                "userSpeciality":$("#userSpeciality").val(),
                "userEname":$("#userEname").val(),
                "userBz":$("#userBz").val()
            }
        }else{
            notesdata = {
                "userId":$scope.userRow.userId,
                "qy":$("#qy").prop("checked")+"",
                "userNumber":$("#userNumber").val(),
                "userName":$("#userName").val(),
                "deptId":$("#deptId").val(),
                "userSex":$scope.lists[$("#userSex").val()],
                "userJob":$("#userJob").val(),
                "userTel":$("#userTel").val(),
                "userEducation":$("#userEducation").val(),
                "userSchool":$("#userSchool").val(),
                "userSpeciality":$("#userSpeciality").val(),
                "userEname":$("#userEname").val(),
                "userBz":$("#userBz").val()
            }
        }
        var info="";
        toaster.postInfo('/a_yongan/add/user',notesdata).then(function(data){
            if(data && data.data && data.data.success){
                showMessage('seccess','','保存成功！');
                $scope.usersAdd = !($scope.usersAdd);
                $scope.usersList = !($scope.usersList);
                getDeptList();
                getUserList();
            }else{
                showMessage('seccess','','保存失败！');
                info = "请求错误！";
            }
        });
        return info;
    }
    inIt();
}]);